<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        html {
            height: 5000px;
        }
    </style>
</head>

<body>

</body>
<script>
    // 节流 => 重复触发 => 降频
    // window.onscroll = function () {
    //     console.log(1111); //期望执行内容
    // }

    // Date.now() => 获取当前时间对应毫秒数   => 等价于 new Date().getTime();

    // 优化1
    // var start = Date.now();  // 页面加载时的当前时间(ms)
    // window.onscroll = function () {
    //     var now = Date.now(); //滚动时的当前时间(ms)
    //     // console.log(now);
    //     if (now - start >= 300) {
    //         console.log(1111); //期望执行内容

    //         start = now;
    //     }
    // }


    // var start = Date.now();  // 页面加载时的当前时间(ms)
    // window.onscroll = function () {
    //     var now = Date.now(); //滚动时的当前时间(ms)
    //     // console.log(now);
    //     if (now - start >= 300) {
    //         console.log(1111); //期望执行内容

    //         start = now;
    //     }
    // }


    window.onscroll = throtten(function () {
        console.log(11111);
    })

    function throtten(callback, delay = 300) {
        var start = Date.now();
        return function () {
            var now = Date.now(); //滚动时的当前时间(ms)
            if (now - start >= delay) {
                // console.log(1111); //期望执行内容
                callback.call(this)
                start = now;
            }
        }
    }

</script>

</html>